package view.itemRenderes.mediators
{
	import events.library.EventSetLibraryItemProperty;
	
	import flash.events.Event;
	
	import model.ModelLibrary;
	import model.ModelRooms;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	
	import org.flashIso.engine.base.IsoPerspective;
	import org.flashIso.engine.library.objects.LBase;
	import org.robotlegs.mvcs.Mediator;
	
	import view.itemRenderes.ItemRendererLObject;
	
	public class MediatorItemRendererLObject extends Mediator
	{
		[Inject]
		public var v:ItemRendererLObject;
		
		[Inject]
		public var ml:ModelLibrary;
		
		[Inject]
		public var mr:ModelRooms;
		
		override public function onRegister() : void  {
			eventMap.mapListener(v, Event.CHANGE, labelChange, Event);
			BindingUtils.bindSetter(setPerspective, mr, "currentPerspective", false, false);
		}
		private function setPerspective (obj:Object) :void {
			if (mr.currentPerspective != null){
				v.isoMap.perspective = mr.currentPerspective;
			}else{
				v.isoMap.perspective = new IsoPerspective();
			}
		}
		
		private function labelChange(e:Event) : void {
			if (ml.getLObjectByID(v.textInput.text) != null){
				Alert.show("An object with this id already exists!","Rename Object");
			}else{
				dispatch(new EventSetLibraryItemProperty(EventSetLibraryItemProperty.CHANGE_LIBRARY_ITEM_PROPERTY, v.data as LBase, "id", v.textInput.text));
			}
			
		}
	}
}